<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

aos_header( 'News' );

if( isset( $_GET[ 'type' ] ) ) {
	$type = $_GET[ 'type' ];
    $query = "SELECT * FROM news WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND type = " . $type . " ORDER BY turn DESC";
    $result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
    $num = mysql_numrows( $result );
    print "<div class=\"blok\">";
    print translate( "Category" ) . ":<br />";
    $operations_link = "<a href=\"news_list.php?type=0\">" . translate( 'Operations' ) . "</a>";
    $military_link = "<a href=\"news_list.php?type=1\">" . translate( 'Military' ) . "</a>";
    $civilian_link = "<a href=\"news_list.php?type=2\">" . translate( 'Civilian' ) . "</a>";
    
    $type_name = "";
    if( $type == 0 ) {
    	$operations_link = $type_name = translate( 'Operations' );
    }
    else if( $type == 1 ) {
    	$military_link = $type_name = translate( "Military" );
    }
    else if( $type == 2 ) {
    	$civilian_link = $type_name = translate( "Civilian" );
    }
    
    print "<a href=\"news.php\">" .translate( 'Unread' ) . "</a> $operations_link $military_link $civilian_link<br />\r\n";
    print "</div><br />\r\n";
    
    if( $num > 0 ) {
	    print "<form method=\"post\" action=\"news_action.php?type=$type\"><table><th colspan=\"3\">" . $type_name . " " .translate( 'news' ) . ":</th>\r\n";
	    print "<tr><td>" .translate( 'Select' ) . "</td><td>" .translate( 'Subject' ) . "</td><td>" .translate( 'Date' ) . "</td></tr>\r\n";
	    $i = 0;
	    while ( $i < $num ) {
	        if( $i % 2 == 0 ) {
	            print "<tr id=\"even\">";
	        }
	        else {
	            print "<tr>";
	        }
	        
	        if( mysql_result( $result, $i, "seen" ) == "0" ) {
	        	$seen = "<strong>";
	        	$seen2 = "</strong>";
	        }
	        else {
	        	$seen = "";
	        	$seen2 = "";
	        }
	
	        print "<td><input type=\"checkbox\" name=\"$i\" id=\"checkbox$i\" /></td><td><a href=\"news_item.php?id=" . mysql_result( $result, $i, "news_id" ) . "\">" . $seen . mysql_result( $result, $i, "subject" ) . $seen2 . "</a></td><td>" .  mysql_result( $result, $i, "date" );
	        print "</td></tr>\r\n";
	        $i ++;
	    }
	    print "</table>";
	    print "<a href=\"javascript:void();\" onclick=\"setSelect(true);\">" .translate( 'select all' ) . "</a> ";
	    print "<a href=\"javascript:void();\" onclick=\"setSelect(false);\">" .translate( 'select none' ) . "</a> ";
	    print "<input type=\"submit\" value=\"" .translate( 'mark read' ) . "\" name=\"read\" /> ";
	    print "<input type=\"submit\" value=\"" .translate( 'mark unread' ) . "\" name=\"unread\" /> ";
	    print "<input type=\"submit\" value=\"" .translate( 'delete' ) . "\" name=\"delete\" /></form>";
    }
    else {
 		print( "<div class=\"blok\">\r\n" );
		print( translate( "You have no" ) . " " . strtolower( $type_name ) . " " . translate( "news items" ) . ".<br />\r\n" );
		print( "</div>\r\n" );
    }
}
else {
?>
					<div class="blok">
						<?php  transprint( 'No news type selected.' ); ?>
					</div>
<?php   
}
?>
					<script language="JavaScript" type="text/javascript">
						function setSelect( toggle ) {
							var still = true;
							var i = 0;
							while ( still ) {
								var tmp = document.getElementById( "checkbox" + i );
								if( !tmp ) {
									still = false;
								}
								else {
									tmp.checked = toggle;
								}
								i ++;
							}
						}
					</script>
<?php
aos_footer();
?>
